package com.rpa.server.servicerpaserver.crawler.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rpa.server.servicerpaserver.crawler.entity.Score;
import com.rpa.server.servicerpaserver.crawler.entity.Work;
import com.rpa.server.servicerpaserver.crawler.service.ScoreService;
import com.rpa.server.servicerpaserver.crawler.service.WorkService;
import com.rpa.server.servicerpaserver.crawler.mapper.WorkMapper;
import com.rpa.server.servicerpaserver.expose.dto.WorkDto;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class WorkServiceImpl extends ServiceImpl<WorkMapper, Work>
    implements WorkService{
    @Autowired
    private ScoreService scoreService;

    @Override
    public List<Map<String, Object>> getWorkList(WorkDto workDto) {
         LambdaQueryWrapper<Work> workLambdaQueryWrapper = new LambdaQueryWrapper<Work>()
                .eq(Work::getAccountId, workDto.getAccountId())
                .eq(Work::getCourseId, workDto.getCourseId())
                .eq(Work::getClazzId, workDto.getClazzId())
                .eq(Work::getIsEnabled,1);
         if (StrUtil.isNotBlank(workDto.getWorkNameLike())){
             workLambdaQueryWrapper.like(Work::getWorkName,workDto.getWorkNameLike());
         }
         if(CollectionUtil.isNotEmpty(workDto.getStudentNumberIn())){
             workLambdaQueryWrapper.in(Work::getStudentNumber,workDto.getStudentNumberIn());
         }
         List<Map<String, Object>> list = baseMapper.getWorkList(workLambdaQueryWrapper);
        return list;
    }

    @Override
    public boolean updateWorkEnum(List<Map> workEnumList) {
        boolean b = updateEnum(workEnumList);
        if (b){
            WorkDto workDto = getWorkDto(workEnumList);
            List<Map<String, Object> > list = getWorkScore(workDto);
            if (Collections.isEmpty(list)){
                return false;
            }
            for (Map map : list) {
                String accountId = MapUtil.getStr(map, "accountId", "");
                String courseId = MapUtil.getStr(map, "courseId", "");
                String clazzId = MapUtil.getStr(map, "clazzId", "");
                double totalScore = Double.parseDouble(MapUtil.getStr(map, "totalScore", ""));
                String userName = MapUtil.getStr(map, "userName", "");
                String  studentNumber= MapUtil.getStr(map, "studentNumber", "");
                Score build = Score.builder()
                        .accountId(accountId)
                        .courseId(courseId)
                        .clazzId(clazzId)
                        .scoreNumber(totalScore)
                        .userName(userName)
                        .scoreType(4)
                        .studentNumber(studentNumber)
                        .build();
                if (existsScore(accountId, courseId, clazzId, userName, 4)){
                    b= scoreService.updateScore(build)&&b;
                }else {
                    b= scoreService.saveOrUpdate(build)&&b;
                }
            }
        }
        return b;
    }

    @Override
    public List<List<String>> head() {
        List<List<String>> list = Arrays.asList(
                java.util.Collections.singletonList("姓名"),
                java.util.Collections.singletonList("学号"),
                java.util.Collections.singletonList("成绩"));
        return list;
    }

    @Override
    public List<List<Object>> dataList(WorkDto workDto) {
        List<List<Object>> list = new ArrayList<>();
        List<Map<String, Object> > list1 = this.getWorkList(workDto);
        for (Map<String, Object> work : list1){
            List<Object> row = new ArrayList<>();
            row.add(MapUtil.getStr(work, "userName","-"));
            row.add(MapUtil.getStr(work, "studentNumber","-"));
            row.add(MapUtil.getStr(work, "score","-"));
            list.add(row);
        }
        return list;
    }
    public boolean updateEnum(List<Map> workEnumList) {
        boolean b = true;
        for(Map workEnum : workEnumList){
            LambdaUpdateWrapper<Work> workLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            LambdaUpdateWrapper<Work> set = workLambdaUpdateWrapper.eq(Work::getAccountId, workEnum.get("accountId"))
                    .eq(Work::getCourseId, workEnum.get("courseId"))
                    .eq(Work::getClazzId, workEnum.get("clazzId"))
                    .eq(Work::getWorkName, workEnum.get("workName"))
                    .eq(Work::getWorkTime, workEnum.get("workTime"))
                    .set(Work::getProportion, workEnum.get("proportion"))
                    .set(Work::getIsEnabled, workEnum.get("isEnabled"));
            b= b && this.update(set);
        }
        return b;
    }
    public List<Map<String, Object> > getWorkScore(WorkDto workDto) {
        LambdaQueryWrapper<Work> workLambdaQueryWrapper = new LambdaQueryWrapper<Work>()
                .eq(Work::getAccountId, workDto.getAccountId())
                .eq(Work::getCourseId, workDto.getCourseId())
                .eq(Work::getClazzId, workDto.getClazzId())
                .eq(Work::getIsEnabled,1);
        return baseMapper.getWorkScore(workLambdaQueryWrapper);
    }
    public WorkDto getWorkDto(List<Map> workEnumList) {
        WorkDto workDto = new WorkDto();
        if (Collections.isEmpty(workEnumList)){
            return workDto;
        }
        Map map = workEnumList.get(0);
        workDto.setAccountId(map.get("accountId").toString());
        workDto.setCourseId(map.get("courseId").toString());
        workDto.setClazzId(map.get("clazzId").toString());
        return workDto;

    }
    public boolean existsScore(String accountId, String courseId, String clazzId,String userName,int scoreType){
        LambdaQueryWrapper<Score> scoreLambdaQueryWrapper = new LambdaQueryWrapper<Score>()
                .eq(Score::getAccountId, accountId)
                .eq(Score::getCourseId, courseId)
                .eq(Score::getClazzId, clazzId)
                .eq(Score::getUserName, userName)
                .eq(Score::getScoreType, scoreType);
        return scoreService.exists(scoreLambdaQueryWrapper);
    }

}




